package com.jboltai.resource.vdb;

import com.alibaba.fastjson.JSONObject;
import com.jboltai.capability.vdb.collection.VDBCollection;
import com.jboltai.resource.Resource;
import com.jboltai.resource.embedding.EmbeddingModel;

public abstract class VDBResource extends Resource {



    /**
     * 连接地址
     */
    protected String url;

    /**
     * 连接用户名
     */
    protected String username;

    /**
     * 连接密码
     */
    protected String password;


    /**
     * 数据库类型
     */
    protected VDBType type ;

    /**
     * 使用的数据库
     */
    protected String database;

    /**
     * 使用哪种embedding模型
     */
    protected EmbeddingModel embeddingModel;

    /**
     * 额外的配置
     */
    protected JSONObject config;

    /**
     * 默认 Collection 的分片数。分片是把大数据集切成多个子数据集。在搜索时，全部分片是并发执行的，分片数量越多，平均耗时越低，但是过多的分片会带来额外开销而影响性能。<br/>
     * 单分片数据量建议控制在300万以内，例如500万向量，可设置2个分片。<br/>
     * 默认为 1。
     */
    protected int defaultShardNum = VDBCollection.DEFAULT_SHARD_NUM;

    /**
     * 默认 Collection 的副本数。副本数是指每个主分片有多个相同的备份，用来容灾和负载均衡。<br/>
     * 不是所有向量数据库都支持该参数。
     * 默认值是0
     */
    protected int defaultReplicaNum = VDBCollection.DEFAULT_REPLICA_NUM;

    /**
     * 资源
     * @param id 资源id
     * @param url
     * @param username
     * @param password
     */
    protected VDBResource(String id, VDBType type, String url, String username, String password, EmbeddingModel embeddingModel) {
        this(id, type, url, username, password, null, embeddingModel, null);
    }

    /**
     * 资源
     * @param id
     * @param type
     * @param url
     * @param accessToken
     */
    protected VDBResource(String id, VDBType type, String url, String accessToken, EmbeddingModel embeddingModel) {
        this(id, type, url, null, accessToken, null, embeddingModel, null);
    }



    /**
     * 资源
     * @param id 资源id
     * @param url
     * @param username
     * @param password
     */
    protected VDBResource(String id, VDBType type, String url, String username, String password, String databse, EmbeddingModel embeddingModel, JSONObject config) {
        super(id);
        this.url = url;
        this.username = username;
        this.password = password;
        this.type = type;
        this.database = databse;
        this.embeddingModel = embeddingModel;
        this.config = config;
    }

    protected  <T> T getConfigValue(JSONObject config, String key,Class<T> clazz, T defaultValue) {
        if (config != null && config.containsKey(key)) {
            String value = config.getString(key);
            if (clazz == String.class) {
                return (T) value;
            }
            if (clazz == Integer.class) {
                return (T) Integer.valueOf(value);
            }
            if (clazz == Long.class) {
                return (T) Long.valueOf(value);
            }
            if (clazz == Boolean.class) {
                return (T)Boolean.valueOf(value.equals("true"));
            }
        }
        return defaultValue;
    }



    public String getUrl() {
        return url;
    }


    public String getUsername() {
        return username;
    }


    public String getPassword() {
        return password;
    }

    public VDBType getType() {
        return type;
    }

    public String getDatabase() {
        return database;
    }

    public VDBResource setDatabase(String database) {
        this.database = database;
        return this;
    }

    public EmbeddingModel getEmbeddingModel() {
        return embeddingModel;
    }

    public VDBResource setEmbeddingModel(EmbeddingModel embeddingModel) {
        this.embeddingModel = embeddingModel;
        return this;
    }

    public VDBResource setConfig(JSONObject config) {
        this.config = config;
        return this;
    }

    public JSONObject getConfig() {
        return config;
    }

    public int getDefaultShardNum() {
        return defaultShardNum;
    }

    public VDBResource setDefaultShardNum(int defaultShardNum) {
        this.defaultShardNum = defaultShardNum;
        return this;
    }

    public int getDefaultReplicaNum() {
        return defaultReplicaNum;
    }

    public VDBResource setDefaultReplicaNum(int defaultReplicaNum) {
        this.defaultReplicaNum = defaultReplicaNum;
        return this;
    }
}
